Skip to content

fix(cdk/table): ensure CdkTable updates view with OnPush and trackBy #31451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025

Conversation

skrtheboss
Copy link
Contributor

Fixes #24483

Description

When CdkTable is used with the OnPush change detection strategy and a trackBy function, changes to the dataSource might not trigger a view update if the trackBy function indicates that the row instances themselves haven't changed. This can lead to the data-bound elements within the table rows displaying outdated information, even if their underlying data properties have been modified.

This PR addresses the issue by adding _changeDetectorRef.markForCheck() calls within the renderRows method of CdkTable. These calls ensure that whenever the table's data or row definitions are updated, the component is explicitly marked for change detection. This forces a re-evaluation of the bindings for all visible rows, ensuring the view remains consistent with the latest underlying data.

A new test case has been added to table.spec.ts to specifically verify this behavior in an OnPush component using trackBy when data properties are updated.

When CdkTable is used with OnPush change detection and a `trackBy` function,
changes to the `dataSource` might not trigger a view update if the `trackBy`
function indicates that the rows themselves haven't changed (even if their
internal data has). This can leave the data-bound elements within the
table rows displaying outdated information.

This commit adds `markForCheck()` calls within the `_renderRows` method
of `CdkTable`. These calls ensure that when the table's data is updated
(even without full row re-creation due to `trackBy`), the component is
explicitly marked for change detection. This triggers a re-evaluation
of the bindings for all visible rows, ensuring the view remains
consistent with the underlying data.

Fixes angular#24483
@skrtheboss skrtheboss requested a review from a team as a code owner June 26, 2025 13:30
@skrtheboss skrtheboss requested review from mmalerba and wagnermaciel and removed request for a team June 26, 2025 13:30
@mmalerba mmalerba added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Aug 11, 2025
@mmalerba mmalerba merged commit 5b45df3 into angular:main Aug 15, 2025
29 checks passed
@atsjo
Copy link

atsjo commented Aug 16, 2025

hoping this will also be merged into 20.2 @mmalerba , as of now it's only in main... I have this problem, and hoping it will be resolved before v21

@skrtheboss skrtheboss deleted the 24483/cdk-table-onpush-trackby branch August 18, 2025 06:00
mmalerba added a commit to mmalerba/components that referenced this pull request Aug 18, 2025
@mmalerba
Copy link
Contributor

This actually has to be reverted due to introducing an infinite change detection cycle in a google app. We can try to re-land it after investigating the issue

mmalerba added a commit that referenced this pull request Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action: merge The PR is ready for merge by the caretaker area: cdk/table target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(mat-table): MatTable with observable as data source should trigger change detection after update
3 participants